import requests
from lxml import etree
import threading
from urllib import request
from queue import Queue
import os
import time

class AutoHome(threading.Thread):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
    }
    def __init__(self,page_queue,detail_queue,*args,**kwargs):
        super(AutoHome, self).__init__(*args,**kwargs)
        self.page_queue = page_queue
        self.detail_queue = detail_queue
        self.session = requests.Session()

class Producer(AutoHome):
    def run(self):
        while True:
            if self.page_queue.empty():
                break
            url = self.page_queue.get()
            self.parse_page(url)
    def parse_page(self,url):
        response = self.session.get(url,headers=self.headers)
        text = response.text
        html = etree.HTML(text)
        links = html.xpath("//div[@class='uibox']//li//img/@src")
        #https://car2.autoimg.cn/cardfs/product/g30/M08/42/54/t_autohomecar__ChsEoFuqOkGAFZ1EAAikBJ_JgBI973.jpg 这是缩略图
        #https://car2.autoimg.cn/cardfs/product/g30/M08/42/54/1024x0_1_q87_autohomecar__ChsEoFuqOkGAFZ1EAAikBJ_JgBI973.jpg 高清图
        #差别就在于 t_autohomecar  1024x0_1_q87_autohomecar

        for link in links:
            img_name = link.split("__")[-1]
            img_url = "https:"+link.replace("t_autohomecar","1024x0_1_q87_autohomecar")
            self.detail_queue.put((img_name,img_url))
            time.sleep(3)

class Customer(AutoHome):
    def run(self):
        while True:
            img = self.detail_queue.get(timeout=60)
            self.download_image(img)
    def download_image(self,img):
        img_name,img_url = img
        request.urlretrieve(img_url,os.path.join("bmw5",img_name))
        print(img_name+"下载完成")


def main():
    page_queue = Queue(15)
    detail_queue = Queue(200)
    urls = [
        'https://car.autohome.com.cn/pic/series/65-1.html#pvareaid=2042222',
        'https://car.autohome.com.cn/pic/series/65-10.html#pvareaid=2042222',
        'https://car.autohome.com.cn/pic/series/65-3.html#pvareaid=2042222',
        'https://car.autohome.com.cn/pic/series/65-12.html#pvareaid=2042222',
        'https://car.autohome.com.cn/pic/series/65-51.html#pvareaid=2042222',
        'https://car.autohome.com.cn/pic/series/65-14.html#pvareaid=2042222',
        'https://car.autohome.com.cn/pic/series-t/65-13.html#pvareaid=2042222',
    ]

    for url in urls:
        page_queue.put(url)

    for x in range(2):
        t = Producer(page_queue,detail_queue)
        t.start()

    for x in range(2):
        t = Customer(page_queue,detail_queue)
        t.start()

if __name__ == "__main__":
    main()


